/*
 * CTalentTree.java
 *
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 *
 * This file is part of JRogueDPS.
 *
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package talents;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.*;
import utility.CUtility;

/**
 *
 * @author Mani
 */
public class CTalentTree {

    final static int c_imageWidth = 230;
    final static int c_imageHeight = 500;
    final static int c_initialXOffset = 8;
    final static int c_initialYOffset = 10;

    private ArrayList<CTalent> m_talentArray;
    private String m_name;
    private JLabel m_treeImage;
    private JLabel m_treeText;
    private ImageIcon m_treeIcon;
    private int m_leftToRightIndex;

    CTalentTree(String p_name, int p_leftToRightIndex)
    {
        m_name = p_name;
        m_talentArray = new ArrayList<CTalent>();
        m_treeImage = new javax.swing.JLabel();
        m_leftToRightIndex = p_leftToRightIndex;

        ClassLoader cl = getClass().getClassLoader();
        ImageIcon l_treeIcon = new ImageIcon(cl.getResource("jroguedps/images/" + p_name.toLowerCase() + "_background.jpg"));
        m_treeIcon = new ImageIcon(CUtility.getScaledInstance(CUtility.getBufferedImageFromImageIcon(l_treeIcon), c_imageWidth, c_imageHeight, RenderingHints.VALUE_INTERPOLATION_BICUBIC, true));
            
        // Setup position of Tree image
        m_treeImage = new javax.swing.JLabel();
        m_treeImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(40, 40, 40), 1, true));
        m_treeImage.setMaximumSize(new java.awt.Dimension(c_imageWidth, c_imageHeight));
        m_treeImage.setMinimumSize(new java.awt.Dimension(c_imageWidth, c_imageHeight));
        int l_xPos = c_initialXOffset + (p_leftToRightIndex * c_imageWidth);
        int l_yPos = c_initialYOffset;
        m_treeImage.setBounds(l_xPos, l_yPos, c_imageWidth, c_imageHeight);
        m_treeImage.setIcon(m_treeIcon);

        m_treeText= new javax.swing.JLabel();
        m_treeText.setBackground(new java.awt.Color(0, 0, 0));
        m_treeText.setFont(new java.awt.Font("Arial Narrow", java.awt.Font.BOLD, 20)); // NOI18N
        m_treeText.setForeground(new java.awt.Color(50, 151, 151));
        m_treeText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        m_treeText.setText(p_name);
        m_treeText.setOpaque(false);
        m_treeText.setBounds(l_xPos, l_yPos, c_imageWidth, 40);
    }
    
    public void addTalent(CTalent p_talent)
    {
        m_talentArray.add(p_talent);
    }

    public CTalent getTalent(int p_index)
    {
        return m_talentArray.get(p_index);
    }

    public int getMaxTalents()
    {
        return m_talentArray.size();
    }
    public int getTotalPoints()
    {
        int l_totalPoints = 0;

        for (int i = 0; i < m_talentArray.size(); i ++)
        {
            l_totalPoints += m_talentArray.get(i).getCurrentPoints();
        }

        return l_totalPoints;
    }

    public void resetTreePoints()
    {
        for (int i = m_talentArray.size() - 1; i >= 0; i--)
        {
            m_talentArray.get(i).setCurrentPoints(0, false);
        }
    }
    
    public CTalent findDependentTalent(String p_name)
    {
        for (int i = 0; i < m_talentArray.size(); i ++)
        {
            CTalent l_talent = m_talentArray.get(i);
            String l_dependency = l_talent.getDependency();
            if (l_dependency != null)
            {
                if (l_dependency.equals(p_name))
                {
                    return l_talent;
                }
            }
        }

        return null;
    }

    public boolean anyTalentsNeedMorePoints(int p_pointsUpToTier)
    {
        for (int i = 0; i < m_talentArray.size(); i ++)
        {
            CTalent l_talent = m_talentArray.get(i);
            if (l_talent.getRequiredPoints() > p_pointsUpToTier &&
                    l_talent.getCurrentPoints() > 0)
            {
                return true;
            }
        }

        return false;
    }

    public int getTotalPointsUpToTier(int p_tier)
    {
        int l_totalPoints = 0;

        for (int i = 0; i < m_talentArray.size(); i ++)
        {
            CTalent l_talent = m_talentArray.get(i);
            if (l_talent.getTier() <= p_tier)
            {
                l_totalPoints += l_talent.getCurrentPoints();
            }
        }

        return l_totalPoints;
    }

    public JLabel getIcon()
    {
        return m_treeImage;
    };

    public JLabel getText()
    {
        return m_treeText;
    };

    public void updateTotalPoints()
    {
        m_treeText.setText(m_name + "(" + getTotalPoints() + ")");
    }
    

}
